/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package themsanpham;

import javafx.scene.CustomNode;
import javafx.scene.text.Text;
import javafx.scene.Node;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.Cursor;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.Stack;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;
import javafx.geometry.Insets;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.ClipView;
import javafx.scene.layout.LayoutInfo;
import themsanpham.ChiTietSanPham;
import javafx.stage.Alert;
import javafx.io.http.HttpRequest;
import javafx.io.http.HttpHeader;
import javafx.io.http.URLConverter;
import java.io.DataInputStream;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javafx.scene.control.TextBox;
import javafx.scene.control.Label;
import javafx.scene.text.Font;
import java.lang.System;

/**
 * @author ToanNhan
 */
public class ChiTietSanPham extends CustomNode {
    public-init var server: String;
    protected var textThemNhomThongTin: Text;
    protected var textLuuChiTietSanPham: Text;
    protected var txtTenNhomSanPham: TextBox;
    protected var arrayNhomThongTin = [NhomThongTinSanPham { Width: 480 }];
    public var vBoxTranslayY: Number;
    protected var stack: Stack;
    protected var vBox: VBox = VBox {
                translateY: bind vBoxTranslayY
                spacing: 5
                padding: Insets { top: 5 left: 5 }
                content: bind arrayNhomThongTin
            }
    protected var scrollBar: ScrollBar = ScrollBar {
                min: 0
                max: bind vBox.height
                vertical: true
                layoutX: 520
                layoutY: 100
                height: 360
            }
    def font: Font = Font.font("Arial", 14);
    protected override function create(): Node {
        return Group {
                    content: [
                        textThemNhomThongTin = Text {
                                    font: Font.font("Arial", 12)
                                    fill: Color.RED
                                    cursor: Cursor.HAND
                                    x: 520 y: 20                                   
                                    content: "Thêm nhóm thông tin"
                                    onMouseClicked: function(e: MouseEvent): Void {
                                        if (e.button == MouseButton.PRIMARY) {
                                            var nhomThongTinSanPham = NhomThongTinSanPham { Width: 480 };
                                            insert nhomThongTinSanPham into arrayNhomThongTin;
                                        }
                                    }
                                    onMouseEntered: function(e: MouseEvent): Void {
                                        textThemNhomThongTin.fill = Color.BLUE;
                                    }
                                    onMouseExited: function(e: MouseEvent): Void {
                                        textThemNhomThongTin.fill = Color.RED;
                                    }
                                }
                        textLuuChiTietSanPham = Text {
                                    font: Font.font("Arial", 12)
                                    fill: Color.RED
                                    cursor: Cursor.HAND
                                    x: 520 y: 40                                   
                                    content: "Lưu chi tiết"
                                    onMouseClicked: function(e: MouseEvent): Void {
                                        if (e.button == MouseButton.PRIMARY) {
                                            var chiTietSanPham = new ChiTietSanPhamClass();
                                            var arrNhomChiTiet = chiTietSanPham.getNhomThongTinSanPhams();
                                            for (nhomChiTiet in arrayNhomThongTin) {
                                                arrNhomChiTiet.add(nhomChiTiet.xuatNhomThongTinSanPham());
                                            }
                                            chiTietSanPham.setTenNhomSanPham("test");
                                            var str = chiTietSanPham.toXml();
                                            if (server.compareTo("") == 0){
                                                server = "http://localhost:8080/WebsiteBanHang/ThemSanPham";
                                            }
                                            var tenNhomSanPham = txtTenNhomSanPham.text;
                                            sendRequest(server, tenNhomSanPham, str);
                                            println(str);
                                        }
                                    }
                                    onMouseEntered: function(e: MouseEvent): Void {
                                        textLuuChiTietSanPham.fill = Color.BLUE;
                                    }
                                    onMouseExited: function(e: MouseEvent): Void {
                                        textLuuChiTietSanPham.fill = Color.RED;
                                    }
                                }
                        Label{
                            font: font
                            layoutY: 5
                            text: "Tên nhóm sản phẩm:"
                        }
                        txtTenNhomSanPham = TextBox{
                            font: font
                            layoutX: 150 columns:38
                        }                        
                        scrollBar,
                        stack = Stack {
                                    layoutY: 25;
                                    padding: Insets { top: 2 left: 2 }
                                    content: [
                                        Rectangle {
                                            //translateY: bind 460 - stack.height
                                            x: 0 y: 0 width: 510 height: 460 arcWidth: 20 arcHeight: 20
                                            fill: Color.WHITE
                                            stroke: Color.BISQUE strokeWidth: 2
                                        }

                                        ClipView {
                                            pannable: false
                                            clipX: 0
                                            clipY: bind scrollBar.value
                                            node: vBox
                                            layoutInfo: LayoutInfo {
                                                width: 500
                                                height: 450
                                            }
                                        }
                                    ]
                                }
                    ]
                }; ;

     ; ; ;                    }

          protected  function  sendRequest    ( url : String, tenNhomSanPham: String, mauDuLieu:  String): Boolean{
        var thanhCong: Boolean = false;
        var urlConverter: URLConverter = URLConverter{};
        var encodingSevletUrl: String = urlConverter.encodeURL(
            "{url}?tennhomsanpham={tenNhomSanPham}&command=themNhomSanPham"
        );
        //var encodingSevletUrl: String = "{url}?tennhomsanpham={tenNhomSanPham}&command=themNhomSanPham";
        System.out.println(encodingSevletUrl);
        var is: InputStream = new ByteArrayInputStream(mauDuLieu.getBytes("utf-8"));
        var httpRequest: HttpRequest = HttpRequest{
            location: encodingSevletUrl
            method: HttpRequest.POST
            headers: [
                HttpHeader{
                    name: HttpHeader.CONTENT_ENCODING
                    value: "utf-8"
                }
                HttpHeader{
                    name: HttpHeader.CONTENT_TYPE
                    value: "text/xml"
                }
                ]
         source:is
         onDone: function(){
             Alert.inform("Đã thêm thành công");
         }


        }
        httpRequest.start();
        return thanhCong;
    }


}
